<?php
	@session_start(); 
	include_once('connection.php');
	
echo '<div class="form1">';
    
	if(isset($_POST['mssv']) && isset($_POST['password']))
	{
		if(empty($_POST['mssv'])) echo '<span class="warn">Vui lòng nhập MSSV</span>';
		else if(empty($_POST['password'])) echo '<span class="warn">Vui lòng nhập mật khẩu</span>';
		else 
		{
			$mssv = $_POST['mssv']; 
			$pass = $_POST['password']; 
			
			$str = "SELECT * FROM sinhvien WHERE mssv = '$mssv' and password = '$pass'";
			$sql = mysql_query($str, $connect);
			
			if(mysql_num_rows($sql) == 0){
				echo '<span class="warn">MSSV hoặc MK không đúng!</span><br />';
			}
			
			else {
				$row = mysql_fetch_assoc($sql);
				$_SESSION['mssv'] = $row['mssv'];
				$_SESSION['pass'] = $row['password'];
			}		
		}	
	}
	
	if(isset($_POST['logout'])) session_unset();

    if(isset($_SESSION['mssv']) && isset($_SESSION['pass']))
    {
		$mssv = $_SESSION['mssv'];
		$pass = $_SESSION['pass'];
?>
		
    	<table>
        	<tr>
            	<td><b><i>MSSV: </i></b></td>
                <td align="right"><?php echo $_SESSION['mssv']?></td>
            </tr>
            <tr>
            	<td><b><i>Họ tên: </i></b></td>
                <td align="right"><?php echo getName($mssv); ?></td>
            </tr>
            <tr>
            	<td><b><i>Lớp: </i></b></td>
                <td align="right"><?php echo getClass($mssv); ?></td>
            </tr>
            <tr><td>----</td>
            </tr>
            <tr>
            	<td></td>
                <td width="140px" align="right">
                    <form action="../monhoc/" method="post">
                        <input type="submit" name="logout" value="Đăng xuất" />
                    </form>
                </td>
            </tr>
        </table>

<?php
    }
    else {
?>	
        <form name="login" action="../monhoc/" method="post">
            <b>Tên truy cập:</b> <span class="warn">*</span><br />
            <input type="text" name="mssv" /> <br />
            <span style="font-style:italic; font-weight:bold; color:#666">
            	Dùng tài khoản chứng thực</span><br /><br />
                
            <b>Mật khẩu:</b> <span class="warn">*</span><br />
            <input type="password" name="password" /> <br />
            <span style="font-style:italic; font-weight:bold; color:#666">
            Dùng mật khẩu chứng thực</span><br /><br />
            
            <input type="submit" value="Đăng nhập" />
        </form>
    

<?php } ?>
</div>
<?php

function getName($id) 
{
	$str = "SELECT * FROM sinhvien WHERE mssv = '$id'";
	$sql = mysql_query($str);
	$row = mysql_fetch_assoc($sql);
	return $row['hoten'];
}

function getClass($id)
{
    $str = "SELECT * FROM sinhvien WHERE mssv = '$id'";
	$sql = mysql_query($str);
	$row = mysql_fetch_assoc($sql);
	$string = $row['makhoa'].$row['khoahoc'];
    return $string;
}
?>